cd "C:\Users\bhighton\Desktop\Dropbox\_My Documents_\Collaborations\asymmetry\analysis\senate\lsq replication materials"
set more off

clear
use "C:\Users\bhighton\Desktop\Dropbox\_My Documents_\Data\CCES 2012\CCES12_Common_VV.dta" 


* converting variable names to lowercase *
rename _all,lower

gen weight=v103
decode inputstate,gen(state)
replace state=ltrim(state)
replace state=rtrim(state)
merge m:1 state using "StateCodesMaster.dta" 
drop _merge

*** demographics ***
recode gender 2=1 else=0,gen(female)
label variable female
recode race 2=1 else=0,gen(black)
label variable black
recode race 3=1 else=0,gen(latino)
label variable latino
recode race 4/8 =1 else=0,gen(othrace)
label variable othrace
recode faminc 32 97=.
recode faminc .=1 else=0,gen(famincna)
label variable famincna
sum faminc ,d
replace faminc=`r(p50)' if faminc==.


*** ideology ***
recode cc334a 8=.,gen(respideo)
label values respideo LABJ
label variable respideo

gen demideo=cc334j if cc334j>=1 & cc334j<=7
label values demideo LABJ
label variable demideo

gen repideo=cc334k if cc334k>=1 & cc334k<=7
replace repideo=. if sencand2party~="Republican"
label values repideo LABJ
label variable repideo


*** PID ***
drop pid3
recode pid7 (1 2 3=0 "Dem") (5 6 7=2 "Rep") (else=1 "Ind/DK"),gen(pid3)
label variable pid3


*** vote choice ***
recode cc410b (1=0 "Dem")(2=1 "Rep")(else=.),gen(vote)
replace vote=. if sencand1party==""
replace vote=. if sencand2party==""
label variable vote

*** Incumbency ***
gen incumb=0
replace incumb=1 if sencand2incumbent=="1"
replace incumb=-1 if sencand1incumbent=="1"
replace incumb=. if sencand1party==""  
replace incumb=. if sencand2party==""

gen year=2012
keep weight-year faminc
compress
save temp1.dta ,replace


*** computing mean positions ***
clear
use temp1.dta
collapse demideo repideo [pw=weight] ,by(state)
drop if demideo==.|repideo==.
gen demideomean=round(demideo,.001)
gen repideomean=round(repideo,.001)
keep state demideomean repideomean
save temp2.dta ,replace


*** merging ***
clear
use temp1.dta
merge m:1 state using temp2.dta
drop _merge

compress
save cces2012_analysis.dta ,replace
erase temp1.dta
erase temp2.dta
